#include "view_times.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "view_time_0.h"
#include "view_time_1.h"
#include "view_time_2.h"
#include "view_time_3.h"
#include "view_time_4.h"
#include "view_time_5.h"
#include "view_time_6.h"
#include "view_time_7.h"
#include "view_time_8.h"
#include "view_time_9.h"
#include "view_time_10.h"
#include "view_time_11.h"
#include "view_time_12.h"
#include "view_time_13.h"
#include "view_time_14.h"
#include "view_time_15.h"
#include "view_time_16.h"
#include "view_time_17.h"
#include "view_time_18.h"
#include "view_time_19.h"
#include "view_time_20.h"
#include "language_text.h"
#include "m_product_test.h"

#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)

static void view_time_base_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 1000;
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
        disp_ctrl.com_data.i_temp[13] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[1] = battery_ctrl_get_level(disp_ctrl.com_data.i_temp[13]);
        disp_ctrl.com_data.i_temp[2] = sport_get_present_step();
        disp_ctrl.com_data.i_temp[3] = sport_get_calorie();
        disp_ctrl.com_data.i_temp[4] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[5] = UIdatetime.time.hour;
        disp_ctrl.com_data.i_temp[6] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[7] = if_time_hour_system_12();
        if (disp_ctrl.com_data.i_temp[7])
        {
            disp_ctrl.com_data.i_temp[7] += (disp_ctrl.com_data.i_temp[5] >= 12); // 1=am,2=pm
            disp_ctrl.com_data.i_temp[5] %= 12;
            if (disp_ctrl.com_data.i_temp[5] == 0)
            {
                disp_ctrl.com_data.i_temp[5] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[8] = UIdatetime.date.year;
        disp_ctrl.com_data.i_temp[9] = UIdatetime.date.month;
        disp_ctrl.com_data.i_temp[10] = UIdatetime.date.day;
        disp_ctrl.com_data.i_temp[11] = get_present_week();
        disp_ctrl.com_data.i_temp[12] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[14] = system_config_get_target_step();
        disp_ctrl.com_data.i_temp[15] = system_config_get_target_calorie();
        disp_ctrl.com_data.i_temp[18] = sport_get_distance();
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[16] = sport_get_distance() * 0.6213712;
        }
        else
        {
            disp_ctrl.com_data.i_temp[16] = sport_get_distance();
        }
        disp_ctrl.com_data.i_temp[17] = system_config_get_target_dist();
        disp_ctrl.com_data.i_temp[20] = home_page_type_get();
    }
    // #warning debug
    //     disp_ctrl.com_data.i_temp[20] = HOME_PAGE_TYPE_1;
    switch (disp_ctrl.com_data.i_temp[20])
    {
#if !defined(TIME_0_SKIP)
    case HOME_PAGE_TYPE_0:
        view_time_0_display();
        break;
#endif
#if !defined(TIME_1_SKIP)
    case HOME_PAGE_TYPE_1:
        view_time_1_display();
        break;
#endif
#if !defined(TIME_2_SKIP)
    case HOME_PAGE_TYPE_2:
        view_time_2_display();
        break;
#endif
#if !defined(TIME_3_SKIP)
    case HOME_PAGE_TYPE_3:
        view_time_3_display();
        break;
#endif
#if !defined(TIME_4_SKIP)
    case HOME_PAGE_TYPE_4:
        view_time_4_display();
        break;
#endif
#if !defined(TIME_5_SKIP)
    case HOME_PAGE_TYPE_5:
        view_time_5_display();
        break;
#endif
#if !defined(TIME_6_SKIP)
    case HOME_PAGE_TYPE_6:
        view_time_6_display();
        break;
#endif
#if !defined(TIME_7_SKIP)
    case HOME_PAGE_TYPE_7:
        view_time_7_display();
        break;
#endif
#if !defined(TIME_8_SKIP)
    case HOME_PAGE_TYPE_8:
        view_time_8_display();
        break;
#endif
#if !defined(TIME_9_SKIP)
    case HOME_PAGE_TYPE_9:
        view_time_9_display();
        break;
#endif
#if !defined(TIME_10_SKIP)
    case HOME_PAGE_TYPE_10:
        view_time_10_display();
        break;
#endif
#if !defined(TIME_11_SKIP)
    case HOME_PAGE_TYPE_11:
        view_time_11_display();
        break;
#endif
#if !defined(TIME_12_SKIP)
    case HOME_PAGE_TYPE_12:
        view_time_12_display();
        break;
#endif
#if !defined(TIME_13_SKIP)
    case HOME_PAGE_TYPE_13:
        view_time_13_display();
        break;
#endif
#if !defined(TIME_14_SKIP)
    case HOME_PAGE_TYPE_14:
        view_time_14_display();
        break;
#endif
#if !defined(TIME_15_SKIP)
    case HOME_PAGE_TYPE_15:
        view_time_15_display();
        break;
#endif
#if !defined(TIME_16_SKIP)
    case HOME_PAGE_TYPE_16:
        view_time_16_display();
        break;
#endif
#if !defined(TIME_17_SKIP)
    case HOME_PAGE_TYPE_17:
        view_time_17_display();
        break;
#endif
#if !defined(TIME_18_SKIP)
    case HOME_PAGE_TYPE_18:
        view_time_18_display();
        break;
#endif
#if !defined(TIME_19_SKIP)
    case HOME_PAGE_TYPE_19:
        view_time_19_display();
        break;
#endif
#if !defined(TIME_20_SKIP)
    case HOME_PAGE_TYPE_20:
        view_time_20_display();
        break;
#endif
#if !defined(TIME_21_SKIP)
    case HOME_PAGE_TYPE_21:
        view_time_21_display();
        break;
#endif
#if !defined(TIME_22_SKIP)
    case HOME_PAGE_TYPE_22:
        view_time_22_display();
        break;
#endif
#if !defined(TIME_23_SKIP)
    case HOME_PAGE_TYPE_23:
        view_time_23_display();
        break;
#endif
    default:
#if defined(DEFAULT_TIME)
        home_page_type_set(DEFAULT_HOME_PAGE);
        DEFAULT_TIME();
#else
#warning debug
        view_time_0_display();
#endif
        break;
    }
}

static void view_time_message_display(void);
int32_t view_time_display(int32_t view)
{
    Expects(view >= view_time);
    Expects(view < view_time + VIEW_LEVEL_0_SUB_COUNT);

    switch (view)
    {
    case view_time_base:
        view_time_base_display();
        break;
    case view_time_message:
        view_time_message_display();
        break;
    }
    return 0;
}

view_event_result_t view_time_event(int32_t view, event_param_t event)
{
    Expects(view >= view_time);
    Expects(view < view_time + VIEW_LEVEL_0_SUB_COUNT);

    switch (event.event)
    {
    case BUTTON_EVENT_TP_TAP:
        return view_event_result_handled;
#if defined(BUTTON_MIDDLE)
    case BUTTON_EVENT_LONG_PRESS:
        if (event.source == event_source_button_middle && view == view_time && product_test_state_get() == PRODUCT_TEST_STATE_TEST_ASSEM)
        {
            home_page_type_switch();
            display_show(view_time, DISP_MODE_DOWN_MOVE);
            return view_event_result_handled;
        }
        break;
#elif defined(USE_TOUCHPAD)
    case BUTTON_EVENT_TP_TAP_LONG:
        if (view == view_time && product_test_state_get() == PRODUCT_TEST_STATE_TEST_ASSEM)
        {
            home_page_type_switch();
            display_show(view_time, DISP_MODE_DOWN_MOVE);
        }
        return view_event_result_handled;

#endif

    case event_view_entry:
    {
        if (view == view_time_message && event.previous_view != view_time_message)
        {
            ancs_disp_ctrl.new_flag = 1;
            ancs_disp_ctrl.read_num = 0;
            if (ancs_msg_get_count() == 0)
            {
                uint8_t *buff = &disp_ctrl.com_data.uint8_temp[201];
                uint32_t buff_len = disp_ctrl.com_data.uint8_temp + sizeof(disp_ctrl.com_data.uint8_temp) - buff;
                disp_ctrl.com_data.uint8_temp[200] = get_language_text(language_text_index_No_Message, buff, buff_len);
            }
            return view_event_result_handled;
        }
        break;
    }
    case event_move_level_down:
    {
        if (view != view_time_base)
        {
            display_show(view_time, DISP_MODE_NORMAL);
            return view_event_result_handled;
        }
        break;
    }
    case event_move_previous_view:
    {
        if (view == view_time_message)
        {
            if (ancs_msg_get_count() == 0)
            {
                display_show(view_time, DISP_MODE_NORMAL);
            }
            else
            {
                ancs_disp_ctrl.disp_offset = 0;
                ancs_disp_ctrl.new_flag = 1;
                if (ancs_disp_ctrl.read_num == 0)
                {
                }
                else
                {
                    ancs_disp_ctrl.read_num--;
                }
                display_show(view_time_message, DISP_MODE_NORMAL);
            }
            return view_event_result_handled;
        }
        break;
    }
    case event_move_next_view:
    {
        if (view == view_time_message)
        {
            if (ancs_msg_get_count() == 0)
            {
                display_show(view_time, DISP_MODE_NORMAL);
            }
            else
            {
                int new_offset = ancs_disp_ctrl.disp_offset + ancs_disp_ctrl.next_offset; //偏移计算
                if (new_offset >= ancs_disp_ctrl.message_len)
                {
                    if (ancs_disp_ctrl.read_num + 1 >= ancs_msg_get_count())
                    {
                        // ancs_disp_ctrl.read_num = 0;
                    }
                    else
                    {
                        ancs_disp_ctrl.disp_offset = 0;
                        ancs_disp_ctrl.new_flag = 1;
                        ancs_disp_ctrl.read_num++;
                        display_show(view_time_message, DISP_MODE_DOWN_MOVE);
                    }
                }
                else
                {
                    ancs_disp_ctrl.disp_offset = new_offset;
                    display_show(view_time_message, DISP_MODE_DOWN_MOVE);
                }
            }
            return view_event_result_handled;
        }
        break;
    }
    }

    return view_event_result_not_handled;
}

/*********************************************************************************************
 * @brief
 * @param [in] param1
 * @param [out] param2
 * @return ret
 * @note
 * @warning
 ********************************************************************************************/
// 36 86 168 120
#define TEXT_X1 36
#define TEXT_Y1 86
#define TEXT_X2 (TEXT_X1 + 168)
#define TEXT_Y2 (TEXT_Y1 + 120)
static void view_time_message_display(void)
{
    uint16_t *unicode_str = (uint16_t *)(&disp_ctrl.com_data.i_temp[4]);
    if (disp_ctrl.loop_cnt == 0) //更新数据需求
    {
        disp_ctrl.auto_refresh_time = 1000;
        if (ancs_msg_get_count() == 0)
        {
            disp_ctrl.com_data.int16_temp[5] = 0;
            if (disp_ctrl.auto_refresh_count >= 3)
            {
                display_show(view_time, DISP_MODE_NORMAL);
                return;
            }
        }
        else
        {
            disp_ctrl.com_data.int16_temp[5] = 1;
            if (ancs_disp_ctrl.new_flag)
            {
                ancs_message_package_t msg = {0};
                ancs_msg_get(&msg, ancs_disp_ctrl.read_num);
                ancs_disp_ctrl.message_type = msg.ancs_msgType;
                LOG("get_msg[%d][%s]", ancs_disp_ctrl.read_num, msg.ancs_msg.appAttrMessage);
                utf8_string_to_unicode_string((uint8_t *)msg.ancs_msg.appAttrMessage, (uint8_t *)unicode_str,
                                              ((uint8_t *)(disp_ctrl.com_data.i_temp)) + sizeof(disp_ctrl.com_data.i_temp) - (uint8_t *)unicode_str);
                disp_ctrl.com_data.int16_temp[4] = arabic_reshaper(unicode_str);
                ancs_disp_ctrl.message_len = wcslen(unicode_str) * 2;
                ancs_disp_ctrl.new_flag = 0;
                ancs_disp_ctrl.disp_offset = 0;
            }
            disp_ctrl.com_data.int16_temp[0] = ancs_disp_ctrl.disp_offset;
            disp_ctrl.com_data.int16_temp[1] = 0;
            disp_ctrl.com_data.int16_temp[2] = 0;
            ancs_disp_ctrl.next_offset = disp_show_font_string_page_cal(TEXT_X1, TEXT_Y1, TEXT_X2, TEXT_Y2, unicode_str, 16) & 0xffff;
            // LOG("msg_offset[%d]", disp_ctrl.com_data.int16_temp[0]);
        }
    }
    if (!disp_ctrl.com_data.int16_temp[5])
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 53 + 31, 62, FILE_IMG_D116_nomessage_53_62_31_0);
        int x = 53;
        int y = 62 + 89;
        if (disp_ctrl.com_data.uint8_temp[200])
        {
            y -= 1;
            disp_show_font_string_utf8_mid(y, (char const *)(&disp_ctrl.com_data.uint8_temp[201]), 0);
        }
        else
        {
            _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_D116_nomessage_53_62_0_89);
        }
        return;
    }
    const uint8_t message_img_tab[] = {FILE_IMG_D115_other_100_11,     FILE_IMG_D118_message_100_11,  FILE_IMG_D106_weixin_100_11,    FILE_IMG_D105_qq_100_11,
                                       FILE_IMG_D105_qq_100_11,        FILE_IMG_D107_facebook_100_11, FILE_IMG_D112_messenger_100_11, FILE_IMG_D109_whatsapp_100_11,
                                       FILE_IMG_D108_twitter_100_11,   FILE_IMG_D113_skype_100_11,    FILE_IMG_D110_line_100_11,      FILE_IMG_D121_in_100_11,
                                       FILE_IMG_D111_instagram_100_11, FILE_IMG_D120_tim_100_11,      FILE_IMG_D119_snapchat_100_11,  FILE_IMG_D115_other_100_11};

    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 100, 25, message_img_tab[ancs_disp_ctrl.message_type % sizeof(message_img_tab)]);

    // LOG("x1[%d] y1[%d] offset[%d] offset_str[%d]", TEXT_X1, TEXT_Y1 + disp_ctrl.com_data.int16_temp[1], disp_ctrl.com_data.int16_temp[0], disp_ctrl.com_data.int16_temp[2]);
    uint32_t temp16 = disp_show_font_string(TEXT_X1, TEXT_Y1 + disp_ctrl.com_data.int16_temp[1], TEXT_X2, TEXT_Y2,
                                            (uint16_t *)((uint8_t *)unicode_str + disp_ctrl.com_data.int16_temp[0] + disp_ctrl.com_data.int16_temp[2]), 16, &disp_ctrl.com_data.int16_temp[4]);
    if (temp16)
    {
        disp_ctrl.com_data.int16_temp[2] += (temp16 & 0xffff);
        disp_ctrl.com_data.int16_temp[1] += ((temp16 >> 16) * (FONT_SIZE + FONT_SIZE_VERTICAL_INTERVAL));
        // LOG("offset_str[%d] offset_y[%d]", disp_ctrl.com_data.int16_temp[1], disp_ctrl.com_data.int16_temp[2]);
    }
    if (disp_ctrl.com_data.int16_temp[0] != 0) //非首页的继续显示
    {
        if ((disp_ctrl.com_data.int16_temp[0] + ancs_disp_ctrl.next_offset) < ancs_disp_ctrl.message_len) //非最后一页
        {
            disp_draw_user_image(TEXT_X2 - 16, TEXT_Y2 - 16, IMAGE_USER_FONT_16X16_DOT);
        }
    }
    else
    {
        if (ancs_disp_ctrl.next_offset < ancs_disp_ctrl.message_len) //多页
        {
            disp_draw_user_image(TEXT_X2 - 16, TEXT_Y2 - 16, IMAGE_USER_FONT_16X16_DOT);
        }
    }
    if (ancs_disp_ctrl.read_num == 0 && ancs_disp_ctrl.disp_offset == 0)
    {
    }
    else
    {
        show_button_up();
    }
    if (ancs_disp_ctrl.read_num + 1 == ancs_msg_get_count() && ancs_disp_ctrl.disp_offset + ancs_disp_ctrl.next_offset >= ancs_disp_ctrl.message_len)
    {
    }
    else
    {
        LOG("%d %d %d %d %d", ancs_disp_ctrl.read_num, ancs_msg_get_count(), ancs_disp_ctrl.disp_offset, ancs_disp_ctrl.next_offset, ancs_disp_ctrl.message_len);
        show_button_down();
    }
}
